package top.kpromise.user.crash;

import okhttp3.*;

import java.io.IOException;

public class DingTalkMessage {

    private static final String URL = "https://oapi.dingtalk.com/robot/send?access_token=" +
            "2ce54522b84e1c7193e9998e9b9a2afd859aa7e635354d55a91cc611378dd19a";

    private DingTalkMessageBuilder builder;

    public DingTalkMessage(DingTalkMessageBuilder builder) {
        this.builder = builder;
    }

    public void send() {
        String json = builder.build();

        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        Request request = new Request.Builder().post(body).url(URL).build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.getMessage();
            }

            @Override
            public void onResponse(Call call, okhttp3.Response response) {
                try {
                    ResponseBody responseBody = response.body();
                    if (responseBody != null) {
                        System.out.println(responseBody.string());
                    }
                }catch (Exception ignore){

                }
            }
        });
    }
}
